package com.etcfg.etlaunch.ui;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import com.etcfg.etlaunch.ETLauncherApplication;
import com.etcfg.etlaunch.ETServer;

public class DeleteServerAction extends AbstractAction {

	private final MainWindow mainWindow;
	private final ETLauncherApplication etLauncherApplication;

	public DeleteServerAction(MainWindow mainWindow,
			ETLauncherApplication etLauncherApplication) {
		super();
		this.mainWindow = mainWindow;
		this.etLauncherApplication = etLauncherApplication;
		this.putValue(NAME, "Delete");
		ImageIcon icon = new ImageIcon(getClass().getResource(
				"/com/etcfg/etlaunch/ui/icons/list-remove32.png")); //$NON-NLS-1$

		putValue(Action.LARGE_ICON_KEY, icon);
		putValue(SMALL_ICON, new ImageIcon(getClass().getResource(
				"/com/etcfg/etlaunch/ui/icons/list-remove-16.png")));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		ETServer selectedEtServer = mainWindow.getSelectedEtServer();
		if (selectedEtServer != null) {
			int result = JOptionPane.showConfirmDialog(mainWindow.getFrame(),
					"Delete this server?", "Deleting server",
					JOptionPane.OK_CANCEL_OPTION);
			if (result == JOptionPane.OK_OPTION) {
				etLauncherApplication.deleteServer(selectedEtServer);
				mainWindow.getListModel().removeServer(selectedEtServer);
			}
		}
	}

}
